﻿using System.Web.Mvc;
using System;
using System.Collections;
using ProgrammersFAQ.Models;
using ProgrammersFAQ.wsPlatformService;


namespace ProgrammersFAQ.Controllers
{
    public class PlatformController : AsyncController
    {
        //
        // GET: /Platform/

        public void IndexAsync()
        {
            AsyncManager.OutstandingOperations.Increment();
            using (wsPlatformService.PlatformServiceClient client = new wsPlatformService.PlatformServiceClient())
            {
                client.getPlatformListCompleted += client_getPlatformListCompleted;
                client.getPlatformListAsync();
            }
        }

        public ActionResult IndexCompleted(PlatformModels[] platformModels)
        {
            return View("Index", platformModels);
        }

        [HttpGet]
        public void EditPlatformAsync(int id = 0)
        {
            AsyncManager.OutstandingOperations.Increment();

            if (id == 0)
            {
                AsyncManager.Parameters["platformModel"] = new PlatformModels();
                AsyncManager.OutstandingOperations.Decrement();
            }
            else
            {
                using (PlatformServiceClient client = new PlatformServiceClient())
                {
                       
                }
            }
        }

        public ActionResult EditPlatformCompleted(PlatformModels platformModel)
        {
            return View("EditPlatform", platformModel);
        }

        private void client_getPlatformListCompleted(object sender, wsPlatformService.getPlatformListCompletedEventArgs e)
        {
            PlatformModels[] platformModelList = null;            
            if (e.Error == null)
            {
                PlatformMO[] platformList = e.Result;
                if (platformList != null && platformList.Length > 0)
                {
                    platformModelList = new PlatformModels[platformList.Length];
                    for (int i = 0; i < platformList.Length; i++)
                    {
                        PlatformModels model = new PlatformModels();
                        model.Id = platformList[i].id;
                        model.Platform = platformList[i].platform;
                        platformModelList[i] = model;
                    }
                    AsyncManager.Parameters["platformModels"] = platformModelList;
                    AsyncManager.OutstandingOperations.Decrement();
                    
                }
            }
        }
    }
}